{"componentChunkName":"component---node-modules-rocketseat-gatsby-theme-docs-core-src-templates-docs-query-js","path":"/code-style/Cast-CTS","result":{"data":{"mdx":{"id":"d5617717-7db0-56b0-bfec-548944914ef4","excerpt":"CTS-01C: Optimization of Statements Type Severity Location Gas Optimization Cast.sol:L8 ,  L12 ,  L16 ,  L20 ,  L24 Description: A miniscule optimization of all…","fields":{"slug":"/code-style/Cast-CTS/"},"frontmatter":{"title":"Cast Code Style Findings","description":"Contains all the findings that relate to code style on the contract codebase","image":null,"disableTableOfContents":null},"body":"var _excluded = [\"components\"];\n\nfunction _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }\n\nfunction _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }\n\nfunction _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }\n\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\nvar _frontmatter = {\n  \"title\": \"Cast Code Style Findings\",\n  \"description\": \"Contains all the findings that relate to code style on the contract codebase\"\n};\nvar layoutProps = {\n  _frontmatter: _frontmatter\n};\nvar MDXLayout = \"wrapper\";\nreturn function MDXContent(_ref) {\n  var components = _ref.components,\n      props = _objectWithoutProperties(_ref, _excluded);\n\n  return mdx(MDXLayout, _extends({}, layoutProps, props, {\n    components: components,\n    mdxType: \"MDXLayout\"\n  }), mdx(\"h2\", {\n    \"id\": \"span-idcts-01ccts-01c-optimization-of-statementsspan\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h2\",\n    \"href\": \"#span-idcts-01ccts-01c-optimization-of-statementsspan\",\n    \"aria-label\": \"span idcts 01ccts 01c optimization of statementsspan permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), mdx(\"span\", {\n    id: \"CTS-01C\"\n  }, \"CTS-01C: Optimization of Statements\")), mdx(\"table\", null, mdx(\"thead\", {\n    parentName: \"table\"\n  }, mdx(\"tr\", {\n    parentName: \"thead\"\n  }, mdx(\"th\", {\n    parentName: \"tr\",\n    \"align\": null\n  }, \"Type\"), mdx(\"th\", {\n    parentName: \"tr\",\n    \"align\": null\n  }, \"Severity\"), mdx(\"th\", {\n    parentName: \"tr\",\n    \"align\": null\n  }, \"Location\"))), mdx(\"tbody\", {\n    parentName: \"table\"\n  }, mdx(\"tr\", {\n    parentName: \"tbody\"\n  }, mdx(\"td\", {\n    parentName: \"tr\",\n    \"align\": null\n  }, mdx(\"a\", {\n    parentName: \"td\",\n    \"href\": \"/reports/maverick-protocol-common-libraries-66165d178c7ae500187a1ad7/appendix/finding-types#gas-optimization\"\n  }, \"Gas Optimization\")), mdx(\"td\", {\n    parentName: \"tr\",\n    \"align\": null\n  }, mdx(\"img\", {\n    parentName: \"td\",\n    \"className\": \"o-severity o-informational\",\n    \"src\": \"https://omniscia.io/report-assets/informational.png\"\n  })), mdx(\"td\", {\n    parentName: \"tr\",\n    \"align\": null\n  }, mdx(\"a\", {\n    parentName: \"td\",\n    \"href\": \"https://github.com/maverickprotocol/maverick-v2/blob/47287a62e15ca8d4bcabf7e0b6757debb5d10593/v2-common/contracts/libraries/Cast.sol#L8\"\n  }, \"Cast.sol:L8\"), \", \", mdx(\"a\", {\n    parentName: \"td\",\n    \"href\": \"https://github.com/maverickprotocol/maverick-v2/blob/47287a62e15ca8d4bcabf7e0b6757debb5d10593/v2-common/contracts/libraries/Cast.sol#L12\"\n  }, \"L12\"), \", \", mdx(\"a\", {\n    parentName: \"td\",\n    \"href\": \"https://github.com/maverickprotocol/maverick-v2/blob/47287a62e15ca8d4bcabf7e0b6757debb5d10593/v2-common/contracts/libraries/Cast.sol#L16\"\n  }, \"L16\"), \", \", mdx(\"a\", {\n    parentName: \"td\",\n    \"href\": \"https://github.com/maverickprotocol/maverick-v2/blob/47287a62e15ca8d4bcabf7e0b6757debb5d10593/v2-common/contracts/libraries/Cast.sol#L20\"\n  }, \"L20\"), \", \", mdx(\"a\", {\n    parentName: \"td\",\n    \"href\": \"https://github.com/maverickprotocol/maverick-v2/blob/47287a62e15ca8d4bcabf7e0b6757debb5d10593/v2-common/contracts/libraries/Cast.sol#L24\"\n  }, \"L24\"))))), mdx(\"h3\", {\n    \"id\": \"description\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#description\",\n    \"aria-label\": \"description permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Description:\"), mdx(\"p\", null, \"A miniscule optimization of all \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://github.com/maverickprotocol/maverick-v2/blob/47287a62e15ca8d4bcabf7e0b6757debb5d10593/v2-common/contracts/libraries/Cast.sol#L7-L9\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"Cast::to\")), \"-prefixed functions can be applied to the codebase by restructuring their statements, increasing their legibility in the process as well.\"), mdx(\"h3\", {\n    \"id\": \"example\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#example\",\n    \"aria-label\": \"example permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Example:\"), mdx(\"pre\", null, mdx(\"code\", {\n    parentName: \"pre\",\n    \"className\": \"language-sol\",\n    \"metastring\": \"title=v2-common/contracts/libraries/Cast.sol highlight={5,9,13,17,21} lineNumbers=true lineOffset=3\",\n    \"title\": \"v2-common/contracts/libraries/Cast.sol\",\n    \"highlight\": \"{5,9,13,17,21}\",\n    \"lineNumbers\": \"true\",\n    \"lineOffset\": \"3\"\n  }, \"library Cast {\\n    error CastValueExceedsBits(uint256 amount, uint256 bits);\\n\\n    function toUint8(uint256 x) internal pure returns (uint8 y) {\\n        if ((y = uint8(x)) != x) revert CastValueExceedsBits(x, 8);\\n    }\\n\\n    function toUint16(uint256 x) internal pure returns (uint16 y) {\\n        if ((y = uint16(x)) != x) revert CastValueExceedsBits(x, 16);\\n    }\\n\\n    function toUint64(uint256 x) internal pure returns (uint64 y) {\\n        if ((y = uint64(x)) != x) revert CastValueExceedsBits(x, 64);\\n    }\\n\\n    function toUint88(uint256 x) internal pure returns (uint88 y) {\\n        if ((y = uint88(x)) != x) revert CastValueExceedsBits(x, 88);\\n    }\\n\\n    function toUint128(uint256 x) internal pure returns (uint128 y) {\\n        if ((y = uint128(x)) != x) revert CastValueExceedsBits(x, 128);\\n    }\\n}\\n\")), mdx(\"h3\", {\n    \"id\": \"recommendation\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#recommendation\",\n    \"aria-label\": \"recommendation permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Recommendation:\"), mdx(\"p\", null, \"We advise the return argument's explicit name to be omitted to prevent memory reservation. As a next step, we advise the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"if\"), \" conditional to evaluate whether the input argument (\", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x\"), \") is greater-than the limitation of the desired type (i.e. for \", mdx(\"a\", {\n    parentName: \"p\",\n    \"href\": \"https://github.com/maverickprotocol/maverick-v2/blob/47287a62e15ca8d4bcabf7e0b6757debb5d10593/v2-common/contracts/libraries/Cast.sol#L7-L9\"\n  }, mdx(\"inlineCode\", {\n    parentName: \"a\"\n  }, \"Cast::toUint8\")), \" the conditional would be \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"x > type(uint8).max\"), \").\"), mdx(\"p\", null, \"After the conditional, the function should simply \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"return\"), \" the value cast directly. These adjustments will result in a \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"6\"), \" unit gas optimization for each instance with optimizations turned on.\"), mdx(\"h3\", {\n    \"id\": \"alleviation-175f8c39b19df69134add3aa8a2a042ce3047763\",\n    \"style\": {\n      \"position\": \"relative\"\n    }\n  }, mdx(\"a\", {\n    parentName: \"h3\",\n    \"href\": \"#alleviation-175f8c39b19df69134add3aa8a2a042ce3047763\",\n    \"aria-label\": \"alleviation 175f8c39b19df69134add3aa8a2a042ce3047763 permalink\",\n    \"className\": \"anchor before\"\n  }, mdx(\"svg\", {\n    parentName: \"a\",\n    \"aria-hidden\": \"true\",\n    \"focusable\": \"false\",\n    \"height\": \"16\",\n    \"version\": \"1.1\",\n    \"viewBox\": \"0 0 16 16\",\n    \"width\": \"16\"\n  }, mdx(\"path\", {\n    parentName: \"svg\",\n    \"fillRule\": \"evenodd\",\n    \"d\": \"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"\n  }))), \"Alleviation (175f8c39b19df69134add3aa8a2a042ce3047763):\"), mdx(\"p\", null, \"All import statements of the \", mdx(\"inlineCode\", {\n    parentName: \"p\"\n  }, \"Cast\"), \" library have been replaced by its OpenZeppelin counterpart which already integrates these optimizations, rendering the exhibit addressed.\"), mdx(ViewDiffButton, {\n    repoUrl: \"https://github.com/maverickprotocol/maverick-v2\",\n    mainHash: \"47287a62e15ca8d4bcabf7e0b6757debb5d10593\",\n    fixHash: \"175f8c39b19df69134add3aa8a2a042ce3047763\",\n    gitHubIssue: \"0\",\n    mdxType: \"ViewDiffButton\"\n  }));\n}\n;\nMDXContent.isMDXComponent = true;","headings":[{"depth":2,"value":"<span id=\"CTS-01C\">CTS-01C: Optimization of Statements</span>"},{"depth":3,"value":"Description:"},{"depth":3,"value":"Example:"},{"depth":3,"value":"Recommendation:"},{"depth":3,"value":"Alleviation (175f8c39b19df69134add3aa8a2a042ce3047763):"}]}},"pageContext":{"slug":"/code-style/Cast-CTS/","prev":{"label":"TransferLib.sol (TLB-M)","link":"/manual-review/TransferLib-TLB"},"next":{"label":"Constants.sol (CST-C)","link":"/code-style/Constants-CST"}}},"staticQueryHashes":["1954253342","2328931024","2501019404","973074209"]}